<?php


namespace app\admin\controller\merchant;


use app\admin\controller\com\Admin;
use app\common\controller\AppCommon;
use app\service\Page;

class Finance extends Admin
{
    //货款记录
    public function index()
    {
        $where = [];
        $orderBy = 'id desc';
        $page = !empty($this->param['page']) ? intval($this->param['page']) : 1;
        $pageSize = 10;
        $type = !empty($this->param['status']) ? intval($this->param['status']) : '';
        $keyword = !empty($this->param['keyword']) ? $this->param['keyword'] : '';
        if ($type !== '') {
            $where['type'] = $type;
        }
        if (!empty($keyword)) {
            if (is_uid($keyword)) {
                $where['uid'] = $keyword;
            } else {
                $muser = AppCommon::data_list_nopage('plugin_merchant', ['user_name|account' => ['like', '%' . $keyword . '%']], 'uid');
                if (!empty($muser)) {
                    $where['uid'] = ['in', array_column($muser, 'uid')];
                } else {
                    $where['uid'] = '';
                }
            }
        }


        if (!empty($type)) {
            $where['type'] = $type;
        }

        $total = AppCommon::data_count('plugin_merchant_credit_log', $where);
        $data = AppCommon::data_list('plugin_merchant_credit_log', $where, $page . ',' . $pageSize, '*', $orderBy);
        if ($total) {
            $merchants = AppCommon::db('plugin_merchant')
                ->where(['uid' => ['in', array_column($data, 'uid')]])
                ->column('user_name,account,uid', 'uid');

            foreach ($data as &$value) {
                $value['merchant'] = null;
                if (isset($merchants[$value['uid']])) {
                    $value['merchant'] = $merchants[$value['uid']];
                }
            }
            unset($value);
        }

        $this->assign('page', Page::set($data, $pageSize, $page, $total, $this->param, url()));
        $this->assign('data', $data);
        return $this->fetch();
    }
}